package com.wms.core.token.login;

import java.util.Hashtable;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

@Component
public class TokenLogin {
	
	private static volatile Hashtable<String, Integer> tokenPool = new Hashtable<>();
	
	private static final Logger logger = Logger.getLogger(TokenLogin.class);
	
	private int code;
	
	private String password;
	
	@Resource
	private FindUserDao dao;
	
	public void setDao(FindUserDao dao) {
		this.dao = dao;
	}
	
	
	public static void deleteToken(String sessionId){
		tokenPool.remove(sessionId);
	}
	
	public static int checkToken(String sessionId){
		return (tokenPool.get(sessionId) == null ? 0 : 1);
	}
	

	public int validateLogInfor(String nam, String pwd, String sessionId){
	
		code = 200;
		try{
			
			code = (nam == null ? 401 : 200);
			
			if(code == 200){
				code = (nam.equals("") ? 402 : 200);
			}
			
			if(code == 200){
				code = (pwd == null ? 403 : 200);
			}
			
			if(code == 200){
				code = (pwd.equals("") ? 404 : 200);
			}
			
			if(code == 200){
				code = (tokenPool.get(sessionId) == null ? 503 : 200);//session是否已有记录
			}
			
			if(code == 503){//没有发现此session记录
				password = dao.getPwdByNam(nam);//根据用户名，查找密码
				
				code = (password == null ? 405 : 503);//账户是否存在
				
				if(code == 503){//账户存在
					code = (password.equals(pwd) ? 503 : 406);//密码是否错误
					
					if(code == 503){//密码正确
						tokenPool.put(sessionId, 1);
					}
				}
				
			}
			
			
		return code;	
		}catch (Exception e){
			e.printStackTrace();
			
			return 577;
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
